﻿using RandomStudent.Mab.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using RandomStudent.Entity;
using RandomStudent.Utilities;

namespace RandomStudent.Controller
{
    public class MenuItemController
    {
        [CommandHandler("FncOpenFileDialog")]
        public void OpenFileDialog()
        {
            MyClass Class7 = new MyClass();
            System.Windows.Forms.FileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Excel文件(*.xslx)|*.xslx|Excel文件(*.xls)|*.xls|所有文件|*.*"; 
            if (fileDialog.ShowDialog() == DialogResult.OK) {
                string filePath = fileDialog.FileName.ToString().Trim();
                LoadDataFromExcel(filePath,Class7);
                XmlHelper.SaveToXml(filePath, Class7, "");
            }
        }

        public void Add()
        {

        }



        #region functions
        /// <summary>
        ///  从excel文件得到数据类
        /// </summary>
        /// <param name="path"></param>
        /// <param name="Class7">表格第一行为标题  此处注意了 </param>
        public void LoadDataFromExcel(string path,MyClass Class7)
        {
            Excel.Application excel = new Excel.Application();
            try
            {
                Excel.Workbook workBook;
                Excel.Worksheet workSheet;
                object oMissing = System.Reflection.Missing.Value;
                workBook = excel.Workbooks.Open(path, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                workSheet = (Excel.Worksheet)workBook.Worksheets[1];

                int rowCount = workSheet.UsedRange.Rows.Count;
                int colCount = workSheet.UsedRange.Columns.Count;

                Excel.Range range;
                //第一列为表头 用英文的 和Student类对应起来  这里可以用反射
                List<string> personParams = new List<string>();
                if (rowCount > 1)
                {
                    for (int i = 1; i <= colCount; i++)
                    {
                        range = excel.get_Range(workSheet.Cells[i], workSheet.Cells[i]);
                        string paramName = range.Value.ToString();
                        personParams.Add(paramName);
                    }
                }
                if (rowCount < 2)
                {
                    return;
                }
                Class7.Students = new List<Student>();
                for (int i = 2; i <= rowCount; i++)
                {
                    Student s1 = new Student();
                    PropertyInfo[] infos = typeof(Student).GetProperties();
                    for (int j = 1; j <= colCount; j++)
                    {
                        range = excel.get_Range(workSheet.Cells[i, j], workSheet.Cells[i, j]);
                        object paramValue = range.Value;
                        PropertyInfo info = infos.First(t => t.Name == personParams[j - 1]);
                        info.SetValue(s1, Convert.ChangeType(paramValue, info.PropertyType));
                    }
                    Class7.Students.Add(s1);
                }
            }
            catch
            {

            }
            finally
            {
                excel.Quit();
            }
        }
        #endregion
    }
}
